<template>
  <el-main class="subject">
    <div>
      <!-- Form -->
      <el-button
        type="primary"
        @click="dialogFormVisible = !dialogFormVisible"
        round
        class="primaryButton"
        style="margin-right: 10px"
        >添加员工
      </el-button>

      <el-dialog
        title="添加员工"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form :model="empForm">
          <el-form-item label="员工号" :label-width="formLabelWidth">
            <el-input
              v-model.number="empForm.empId"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="管理的楼层" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.housId"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empName"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工年龄" :label-width="formLabelWidth">
           <el-input
              v-model.number="empForm.empAge"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工性别" :label-width="formLabelWidth">
            <el-radio v-model="empForm.empSex" label="男" >男</el-radio>
            <el-radio v-model="empForm.empSex" label="女">女</el-radio>
          </el-form-item>
          <el-form-item label="员工电话" :label-width="formLabelWidth">
            <el-input
              v-model.number="empForm.empTel"
              autocomplete="on"
              class="inputSize"
              maxlength="11"
              minlength="8"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工职位" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empPost"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工地址" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empAddr"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empDes"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addEmp()">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="修改学生"
        :visible.sync="dialogFormVisibleByUpdate"
        width="500px"
      >
        <el-form :model="empForm">
          <el-form-item label="员工号" :label-width="formLabelWidth">
            <el-input
              v-model.number="empForm.empId"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="管理的楼层" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.housId"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empName"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工年龄" :label-width="formLabelWidth">
           <el-input
              v-model.number="empForm.empAge"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工性别" :label-width="formLabelWidth">
            <el-radio v-model="empForm.empSex" label="男" >男</el-radio>
            <el-radio v-model="empForm.empSex" label="女">女</el-radio>
          </el-form-item>
          <el-form-item label="员工电话" :label-width="formLabelWidth">
            <el-input
              v-model.number="empForm.empTel"
              autocomplete="on"
              class="inputSize"
              maxlength="11"
              minlength="8"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工职位" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empPost"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工地址" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empAddr"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input
              v-model="empForm.empDes"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateEmp()">确 定</el-button>
        </div>
      </el-dialog>

      <el-button
        type="primary"
        round
        class="primaryButton"
        style="width: 150px"
        @click="getAllEmp()"
        >查询全部员工</el-button
      >
 <el-button
        type="primary"
        round
        class="primaryButton"
        style="width: 150px"
        @click="exportExcelDialog()"
        >导出到excel</el-button
      >

      <el-dialog title="保存路径" :visible.sync="excelFormVisible">
        <div style="margin-left:10%">
          输入您需要保存的文件名称：<el-input type="text" style="width:300px" v-model="excel.excelFileName"/>
          
        </div>
        <div style="margin-left:10%">
            输入您需要保存的目录路径：<el-input type="text" style="width:300px" v-model="excel.excelFilePath"/>       
        </div>
        
        <div slot="footer" class="dialog-footer">
          <el-button @click="excelFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="exportExcel()">确 定</el-button>
        </div>
      </el-dialog>

      <el-radio-group
        v-model="chooseRadio"
        style="width: 300px; margin-left: 15%"
      >
        <el-radio :label="0">按照id查</el-radio>
        <el-radio :label="1">按照名字模糊查询</el-radio>
      </el-radio-group>
      <el-input
        :placeholder="
          chooseRadio === 0 ? PHInFindKey.firstChoose : PHInFindKey.secondChoose
        "
        v-model="findKey"
        style="width: 200px; margin-left: 1%"
        clearable
      >
      </el-input>
      <el-button
        icon="el-icon-search"
        circle
        type="primary"
        style="margin-left: 10px"
        @click="chooseRadio === 0 ? getEmpById() : getEmpByName()"
      ></el-button>
    </div>

    <div style="margin-top: 30px">
      <el-table
        :data="empTableData"
        border
        style="width: 1200px"
        v-loading="loading"
        tooltip-effect="dark"
        @selection-change="selectionChange($event)"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="empId" label="员工号" width="150">
        </el-table-column>
        <el-table-column prop="housId" label="管理的楼号" width="120">
        </el-table-column>
        <el-table-column prop="empName" label="员工姓名" width="120">
        </el-table-column>
        <el-table-column prop="empAge" label="员工年龄" width="120">
        </el-table-column>
        <el-table-column prop="empSex" label="员工性别" width="120">
        </el-table-column>
        <el-table-column prop="empTel" label="员工电话" width="120">
        </el-table-column>
        <el-table-column prop="empPost" label="员工职位" width="120">
        </el-table-column>
        <el-table-column prop="empAddr" label="员工地址" width="120">
        </el-table-column>
        <el-table-column prop="empDes" label="员工描述" width="150">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              @click="dialogFormVisibleShow(scope.row)"
              type="primary"
              circle
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              circle
              size="small"
              @click="deleteEmp(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px; margin-left: 20%">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.pageTotal"
      >
      </el-pagination>
    </div>
  </el-main>
</template>

<script>
import { Alert, Loading } from "element-ui";
export default {
  name: "Emp",
  data() {
    return {
       excel:{
        excelFilePath:"",
        excelFileName:""
      },
      excelFormVisible:false,

      empTableData: [],
      findKey: "",
      dialogFormVisible: false,
      dialogFormVisibleByUpdate: false,
      loading: true,
      multipleSelection: [],
      // empdent表单
      empForm: {
        empId: "",
        housId: "",
        empName: "",
        empAge: "",
        empSex: "",
        empTel: "",
        empPost: "",
        empAddr: "",
        empDes: "",
      },
      formLabelWidth: "130px",
      //单选框选择，默认选择0，id查询
      chooseRadio: 0,
      PHInFindKey: {
        firstChoose: "按照员工号查询",
        secondChoose: "按照姓名模糊查询",
      },
      //匹配对应的方法，1是查询全部，2是查询单个
      eqMethod: {
        methodNum: 0,
      },
      //传输学生数据的默认参数
      emp: {
        empId: "0",
        empName: "0",
      },
      //传输page数据的默认参数
      page: {
        currentPage: 1,
        sizePage: 5,
        pageTotal: 0,
        pageSizes: [3, 5, 8],
      },
      // 传输数据的容器
      objectData: {
        emp: {},
        page: {},
      },
    };
  },
  methods: {
    handleClick(a) {
      console.log(a);
    },
    //选择一次展现几个时候触发
    handleSizeChange(size) {
      console.log("handleSizeChange变换");
      this.page.sizePage = size;
      // this.getAllEmp();
      if (this.eqMethod.methodNum === 1) {
        this.getAllEmp();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getEmpById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getStudentByName();
      }
      console.log("this.page.sizePage" + this.page.sizePage);
    },
    //当前页变化时候触发
    handleCurrentChange(currentPageNum) {
      this.page.currentPage = currentPageNum;
      if (this.eqMethod.methodNum === 1) {
        this.getAllEmp();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getEmpById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getStudentByName();
      }
      // console.log(this.eqMethod.methodNum===1);
      // this.getAllEmp();
    },
    // 获得全部员工
    getAllEmp() {
      this.eqMethod.methodNum = 1;
      console.log(
        this.$globalConfig.testUrl + "emp/getAllEmp/" + this.objectData
      );
      this.objectData.emp = this.emp;
      this.objectData.page = this.page;
      console.log(this.objectData);
      this.$axios
        .post(this.$globalConfig.testUrl + "emp/getAllEmp/", {
          page: this.page,
        })
        .then(
          (response) => {
            console.log("请求成功了", response.data);
            this.empTableData = response.data.extend.empList;
            this.page.currentPage4 = response.data.extend.currentPage;
            this.page.sizePage = response.data.extend.sizePage;
            this.page.pageTotal = response.data.extend.pageTotal;
            this.loading = false;
          },
          (error) => {
            console.log("请求失败了", error.message);
            this.loading = false;
          }
        );
    },
    //按照员工号来查询
    getEmpById() {
      this.eqMethod.methodNum = 2;
      (this.emp.empName = "\'\'"), (this.emp.empId = this.findKey);
      console.log({
        object: this.emp,
        page: this.page,
      });
      if (this.emp.empId != "" && this.chooseRadio === 0) {
        this.$axios
          .post(this.$globalConfig.testUrl + "emp/getOneEmpById", {
            object: this.emp,
            page: this.page,
          })
          .then(
            (response) => {
              console.log("请求成功了", response.data);
              this.empTableData = response.data.extend.empList;
              this.page.currentPage4 = response.data.extend.currentPage;
              this.page.sizePage = response.data.extend.sizePage;
              this.page.pageTotal = response.data.extend.pageTotal;
              this.loading = false;
            },
            (error) => {
              console.log("请求失败了", error.message);
              this.loading = false;
            }
          );
      } else {
        alert("您输入的员工号是空");
      }
    },
    //按照学生姓名模糊查询
    getEmpByName() {
      this.eqMethod.methodNum = 3;
      this.emp.empId = "0";
      this.emp.empName = '"' + this.findKey + '"';
      this.objectData.emp = this.emp;
      this.objectData.page = this.page;

      if (this.emp.empName != "" && this.chooseRadio === 1) {
        this.$axios({
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          url: this.$globalConfig.testUrl + "emp/getSomeEmpByName",
          data: {
            object: this.emp,
            page: this.page,
          },
        }).then(
          (response) => {
            console.log("请求成功了getStudentByName", response.data);
            this.empTableData = response.data.extend.empList;
            this.page.currentPage4 = response.data.extend.currentPage;
            this.page.sizePage = response.data.extend.sizePage;
            this.page.pageTotal = response.data.extend.pageTotal;
            this.loading = false;
          },
          (error) => {
            console.log("请求失败了", error.message);
            alert("请检查您的查询条件")
            this.loading = false;
          }
        );
      } else {
        alert("您输入的员工号是空");
      }
    },
    //添加学生
    addEmp() {
       this.verifyUser();
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "emp/addEmp",
        data: this.empForm,
      }).then(
        (response) => {
          console.log("请求成功了 addEmp", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.getAllEmp();
      this.dialogFormVisible = false;
    },
    //删除学生
    deleteEmp(emp) {
       this.verifyUser();
      console.log(emp);
      // 弹窗提示框
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if(sessionStorage.getItem("rid")==="1"){
          this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "emp/deleteEmp",
            data: { empId: emp.empId },
          }).then(
            (response) => {
              if (response.data.msg === "处理成功")
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              console.log("请求成功了deleteEmp", response.data);
              this.getAllEmp();
            },
            (error) => {
              alert("请检查对应楼层是否存在")
              console.log("请求失败了", error.message);
            }
          );}
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });

      // 将数据恢复原样
      this.emp.empId = "";
    },
    // 展示对话框
    dialogFormVisibleShow(emp) {
      this.empForm = emp;
      this.dialogFormVisibleByUpdate = true;
    },
    // 修改学生
    updateEmp() {
       this.verifyUser();
      console.log(this.empForm);
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "emp/updateEmp",
        data: this.empForm,
      }).then(
        (response) => {
          console.log("请求成功了updateEmp", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllEmp();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.dialogFormVisibleByUpdate = false;
    },
    verifyUser(){
     this.$axios
          .post(this.$globalConfig.testUrl + "admin/verifyUser/", {
          username:sessionStorage.getItem("username"),password:sessionStorage.getItem("password"),rid:sessionStorage.getItem("rid")
          })
          .then(
          (response) => {
              console.log("请求成功了", response.data);
              if(response.data.code === 200){
                  return "ok";
              }else{
                  alert("会话出现问题，请重新登录！");
                  sessionStorage.removeItem('rid');
                  this.$router.push({
                      name:'logout'
                  });
              }

          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
      
    },
    // 这里可以做多个删除
    selectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((a)=>{
        console.log(a.empId);
      })
    },
    exportExcelDialog(){
      console.log("导出到excel");
      this.excelFormVisible = true;
    },
    exportExcel(){
        console.log(this.excel.excelFilePath);
        console.log(this.excel.excelFileName);
        this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "emp/exportExcel",
            data:this.excel ,
          }).then(
            (response) => {
              console.log("请求成功了 exportExcel", response.data);
               this.$message({
                showClose: true,
                message: response.data.extend.fileSaveTips,
                type: 'success'
              });
            },
            (error) => {
              console.log("请求失败了", error.message);
            }
          );
    }
  },
  mounted() {
    this.getAllEmp();
    // console.log("Student被挂载了")
    // this.$bus.$emit('ControlHomeSpan',false);
  },
  beforeDestroy() {
    // console.log("Main被摧毁了")
  },
};
</script>

<style scoped>
.subject {
  position: absolute;
  top: 100px;
  left: 40px;
  flex: auto;
  background-color: white;
}
.primaryButton {
  width: 100px;
  height: 40px;
  font-size: 16px;
}
.inputSize {
  width: 200px;
}
</style>